package com.system.manager.modules.risk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.manager.common.annotation.CurrSubject;
import com.system.manager.modules.authorized.entity.Subject;
import com.system.manager.modules.authorized.entity.User;
import com.system.manager.modules.authorized.service.IUserService;
import com.system.manager.modules.risk.entity.TbFileStaffstation;
import com.system.manager.modules.risk.service.ITbExamService;
import com.system.manager.modules.risk.service.ITbFileService;
import com.system.manager.modules.risk.service.ITbFileStaffstationService;
import com.system.manager.modules.risk.vo.TaskDate;
import com.system.manager.modules.risk.vo.UserDate;
import com.system.manager.modules.risk.entity.StaffStation;
import com.system.manager.modules.risk.entity.TbTask;
import com.system.manager.modules.risk.mapper.TbTaskMapper;
import com.system.manager.modules.risk.service.ITbTaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yxp
 * @since 2021-11-12
 */
@Service
public class TbTaskServiceImpl extends ServiceImpl<TbTaskMapper, TbTask> implements ITbTaskService {
    @Autowired
    private TbTaskMapper tbTaskMapper;
    @Autowired
    private ITbExamService iTbExamService;
    @Autowired
    private IUserService userService;
    @Autowired
    private ITbTaskService iTbTaskService;
    @Autowired
    private ITbFileStaffstationService iTbFileStaffstationService;
    @Autowired
    private ITbFileService iTbFileService;
    //查看学习情况
    @Override
    public String findScore(@CurrSubject Subject subject) {
        UserDate userDate = iTbExamService.findScore(subject);
        /*Integer uId = staffStation.getUId();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("id",uId);
        List<TbTask> taskList = tbTaskMapper.selectList(wrapper);
        UserDate<TbTask> userDate = new UserDate<>();
        userDate.setCode(0);
        userDate.setMsg("教育评估");
        userDate.setCount(1);
        userDate.setData(taskList);*/
        List data = userDate.getData();
        String score = (String) data.get(0);
        return score;
    }

    @Override
    public String findAviScore(@CurrSubject Subject subject) {
        String result = null;
        QueryWrapper wrapper = new QueryWrapper();
        User byId = userService.getById(subject.getId());
        Integer userId = byId.getUserId();
        wrapper.eq("u_id",userId);
        TbTask tbTask = tbTaskMapper.selectOne(wrapper);
        result = tbTask.getAvi();

        return result;
    }

    @Override
    public String findFileScore(@CurrSubject Subject subject) {
        String result = null;
        QueryWrapper wrapper = new QueryWrapper();
        User byId = userService.getById(subject.getId());
        Integer userId = byId.getUserId();
        wrapper.eq("u_id",userId);
        TbTask tbTask = tbTaskMapper.selectOne(wrapper);
        result = tbTask.getFile();
        return result;
    }

    //查询用户的情况
    @Override
    public UserDate<TaskDate> findAll(Subject subject) {
        User byId = userService.getById(subject.getId());
        String name = byId.getName();
        UserDate<TaskDate> userDate = new UserDate<>();

        String examScore = iTbTaskService.findScore(subject);
        String aviScore = iTbTaskService.findAviScore(subject);
        String fileScore = iTbTaskService.findFileScore(subject);

        List<TaskDate> scoreList = new ArrayList();
        TaskDate taskDate = new TaskDate();
        taskDate.setUserName(name);
        taskDate.setExamScore(examScore);
        taskDate.setAviScore(aviScore);
        taskDate.setFileScore(fileScore);
        scoreList.add(taskDate);
        userDate.setCode(0);
        userDate.setMsg("用户的教育任务情况");
        userDate.setCount(1);
        userDate.setData(scoreList);
        return userDate;
    }

    /*
    设置视频学习情况
     */
    @Override
    public void updateAviScore(Integer userId) {
        List<TbFileStaffstation> tbFileStaffstations = iTbFileStaffstationService.selectOkByStaffStation(userId);
        int total = 0;
        int cur = 0;
        for (TbFileStaffstation tbFileStaffstation : tbFileStaffstations) {
            //文件id
            Integer fileId = tbFileStaffstation.getFileId();

            String s = iTbFileService.selectStateByFileId(fileId);
            if(s.equals("视频")){
                total += 1;
                //文件完成情况
                Integer state = tbFileStaffstation.getState();
                if(state == 1){
                    cur += 1;
                }
            }
        }
        if (total == 0){
            return;
        }else {
            double result = cur/total;
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("u_id",userId);
            TbTask task = tbTaskMapper.selectOne(wrapper);
            if(task == null){
                TbTask tbTask = new TbTask();
                tbTask.setUId(userId);
                tbTask.setAvi(String.valueOf(result));
                tbTaskMapper.insert(tbTask);
            }else {
                task.setAvi(String.valueOf(result));
                tbTaskMapper.updateById(task);
            }
        }
    }

    /*
   设置文件学习情况
    */
    @Override
    public void updateFileScore(Integer userId) {
        List<TbFileStaffstation> tbFileStaffstations = iTbFileStaffstationService.selectOkByStaffStation(userId);
        int total = 0;
        int cur = 0;
        for (TbFileStaffstation tbFileStaffstation : tbFileStaffstations) {
            //文件id
            Integer fileId = tbFileStaffstation.getFileId();

            String s = iTbFileService.selectStateByFileId(fileId);
            if(s.equals("文件") || s.equals("图片")){
                total += 1;
                //文件完成情况
                Integer state = tbFileStaffstation.getState();
                if(state == 1){
                    cur += 1;
                }
            }
        }
        if (total == 0){
            return;
        }else {
            double result = cur/total;
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("u_id",userId);
            TbTask task = tbTaskMapper.selectOne(wrapper);
            if(task == null){
                TbTask tbTask = new TbTask();
                tbTask.setUId(userId);
                tbTask.setAvi(String.valueOf(result));
                tbTaskMapper.insert(tbTask);
            }else {
                task.setAvi(String.valueOf(result));
                tbTaskMapper.updateById(task);
            }
        }

        //修改值

    }
}
